let up = document.querySelector("#top");
let lower = document.querySelector("#lower")
let pages = document.querySelector("#pages")
let backTop = document.querySelector("#backTop");
let i = 0

function getUrlParams() {
    let href = location.href; //获取浏览器的地址
    let regExp = /.+\?(.+)/
    let param = regExp.exec(href) //匹配?后面的参数
    if (!param) {
        console.log('没有query参数');
        return
    }
    param = param[1]
        // console.log(param);
        // console.log(decodeURI(param));解码URI的参数编码
    let paramArr = param.split("&"); //多个参数以"&"切割 ?name=李雷&age=30
    console.log(paramArr); //["name=李雷","age=30"]
    let query = {} //声明一个对象
    paramArr.forEach(item => {
        itemArr = item.split("=");
        console.log(itemArr);
        query[itemArr[0]] = decodeURI(itemArr[1])
    })

    return query
}
let query = getUrlParams()

fetch(`http://chst.vip:1234/api/getmoneyctrl?pageid=${query.pageid}`)
    .then(body => body.json())
    .then(res => {
        console.log(res);
        let num = Math.floor(res.totalCount / res.pagesize)
        pages.innerHTML = `${Number(query.pageid)+1}/${num+1}`
        let html = ""
        res.result.forEach(function(itme, index) {
            html += `
            <li productid=${itme.productId}>
            <a href="#">
            ${itme.productImgSm}
            </a>
           <div>
           <a href="#">
           <p>${itme.productName}</p>
        </a>
        <a href="#">
           <p>${itme.productFrom}${itme.productTime}</p>
        </a>
        </div>
        </li>`
        })
        uu1.innerHTML = html;
        lower.onclick = debounce(function() {
            if (query.pageid >= num) {
                location.href = `/src/pages/moneyctrl.html?pageid=${Number(query.pageid)}`;
            } else { location.href = `/src/pages/moneyctrl.html?pageid=${Number(query.pageid)+1}`; }

        }, 500)
        up.onclick = debounce(function() {
            if (query.pageid <= 0) {
                location.href = `/src/pages/moneyctrl.html?pageid=${Number(query.pageid)}`;
            } else {
                location.href = `/src/pages/moneyctrl.html?pageid=${Number(query.pageid)-1}`;
            }
        }, 500);
        [...uu1.children].forEach(li => {
            li.onclick = function() {
                let productid = this.getAttribute('productid')
                console.log(productid);
                location.href = `/src/pages/moneyproduct.html?productid=${productid}`
            }
        })
    })

backTop.onclick = function() {
    document.getElementById('content').scrollTop = 0;
}
document.getElementById('content').onscroll = function() {}